define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        materialList: function () {
            // 更新物料状态
            $('.purchase-status').change(function() {
                var id = $(this).data('id');
                var field = $(this).data('field');
                var value = $(this).val();
                
                Fast.api.ajax({
                    url: 'scanwork/order/updateMaterialStatus',
                    data: {
                        id: id,
                        field: field,
                        value: value
                    }
                }, function(data, ret) {
                    Toastr.success('状态更新成功');
                }, function(data, ret) {
                    Toastr.error(ret.msg);
                });
            });
            
            // 批量更新状态
            $('#update-all-status').click(function() {
                var status = prompt('请选择要批量更新的状态：\n0-待采购\n1-已下单\n2-已到货', '1');
                if (status !== null && /^[0-2]$/.test(status)) {
                    var count = 0;
                    $('.purchase-status').each(function() {
                        if ($(this).val() != status) {
                            $(this).val(status).trigger('change');
                            count++;
                        }
                    });
                    if (count > 0) {
                        Toastr.success('已更新 ' + count + ' 个物料状态');
                    } else {
                        Toastr.info('没有需要更新的物料');
                    }
                }
            });
        }
    };
    return Controller;
});
